//
//  Search.m
//  HYHOME
//
//  Created by Mac on 16/2/8.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Search.h"
#import "DataBaseHandle.h"
#import "SearchTableViewCell.h"
#import "DKNightVersion.h"
#import "NetHandleBlock.h"
#import "PushSearch.h"
#import "ViewController.h"
#import "DKNightVersion.h"
@interface Search ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>


@property (nonatomic, retain) UIView *viewForNail;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UISearchBar *search;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic,retain) UITextField *text;




@end

@implementation Search




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor  =[UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    self.title = @"搜索";
    
    [self createtableView];
    
}


-(void)createtableView{
    
    
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    self.search.delegate = self;
    
    self.search.dk_backgroundColorPicker = DKColorWithColors([UIColor grayColor], [UIColor lightGrayColor]);
    self.search.placeholder = @"输入需要搜索的内容";
    self.search.showsCancelButton = YES;
    
    //注册结果表的cell
    [self.view addSubview:self.search];
    
    [_search release];
    
    
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"f1a0b18c60148b83ad152a0c1f4fc0a7.jpg"]];
    
        imageView.userInteractionEnabled = YES;
        imageView.frame = CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height);
        [self.view addSubview:imageView];
        [imageView release];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height)];
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    
    self.tableView.userInteractionEnabled = YES;
    
    
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    
    
}



- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
        self.tableView.backgroundColor =[UIColor clearColor];
    [self.search resignFirstResponder];
    
    
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
        if (searchText != nil) {
    
            self.tableView.backgroundColor = [UIColor whiteColor];
    
        }else{
    
            self.tableView.backgroundColor = [UIColor clearColor];
        }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    NSString *url =@"http://api.wsq.umeng.com/v2/feed/search?os=iOS&openudid=c4fef44ea2c33987b846791b59bd050a622bea9b&sdkv=2.2.1&ak=5636cff167e58ef91b0001ae&de=iPhone5,2&mac=02:00:00:00:00:00&uid=56cfc0c97019c90972c5ff64&keywords=";
    
    NSString *url2 = @"&topic_id=568b2560ee78505f3e9da44c&count=20";
    self.url = [url stringByAppendingFormat:@"%@%@", self.search.text, url2];
    
    [NetHandleBlock networkingWithURL:self.url completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        
        self.array = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"items"];
        
        for (NSDictionary *dic in arr) {
            
            SearchModel *model = [[SearchModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            model.content = [dic objectForKey:@"content"];
            
            
            
            model.share_link  = [dic valueForKey:@"share_link"];
            
            
            [self.array addObject:model];
            [model release];
        }
        
        
        [self.tableView reloadData];
    }];
    
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.array.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    
    SearchModel *model = [self.array objectAtIndex:indexPath.row];
    
    
    [cell passModel:model];
    
    //     带属性的字符串实现关键字高亮
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:model.content];
    
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[model.content rangeOfString:self.search.text]];
    [cell.content setAttributedText :attribute];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    self.tableView.backgroundColor = [UIColor clearColor];
    return self.view.frame.size.height / 8;
    
    
}



// 判断哪些cell 可以编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return YES;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        [self.array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    PushSearch *search =[[PushSearch alloc]init];
    
    SearchModel *model = [self.array objectAtIndex:indexPath.row ];
    [search passSecondModel:model];
    
    [self.navigationController pushViewController:search animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
